package com.exject.android.geobasedsoundmanager.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.android.maps.GeoPoint;

/**
 * 
 * @author Atracura
 * 
 */
public class SettingsImpl implements Settings {
	/**
	 * Access name for the settings file
	 */
	private static final String PREFS_FILE = "geoBasedSoundManagementConfiguration";
	/**
	 * Access object for the settings
	 */
	private SharedPreferences sharedPreferences;
	/**
	 * Singleton object
	 */
	private static Settings settings;
	/**
	 * Key for the longitude of the starting point
	 */
	private static final String START_POSITION_LONG = "START_POSITION_LONG";
	/**
	 * Key for the latitude of the starting point
	 */
	private static final String START_POSITION_LAT = "START_POSITION_LAT";
	/**
	 * Key for the address of the starting point
	 */
	private static final String START_POSITION_ADDRESS = "START_POSITION_ADDRESS";
	/**
	 * Key for the default value to use as the starting point
	 */
	private static final String START_POSITION_DEFAULT = "START_POSITION_DEFAULT";

	
	/**
	 * Init the class with a singleton object
	 * 
	 * @param context
	 */
	private SettingsImpl(Context context) {
		this.sharedPreferences = context.getSharedPreferences(PREFS_FILE, 0);
	}

	/**
	 * to get the singleton object
	 * 
	 * @return the singleton object
	 */
	public static Settings getInstance() {
		if (SettingsImpl.settings != null) {
			return settings;
		} else {
			throw new NoSettingInitException(
					"no init until yet, please init with the method initInstance(Context context)");
		}
	}
	
	public static boolean singletonAvaible() {
		return SettingsImpl.settings != null;
	}

	/**
	 * init the singleton
	 * 
	 * @param context
	 *            The context of the current project
	 * @return the new Settings object (singleton)
	 */
	public static Settings initInstance(Context context) {
		SettingsImpl.settings = new SettingsImpl(context);
		return SettingsImpl.settings;
	}

	public GeoPoint getStartPosition() {
		int latitude = this.sharedPreferences.getInt(
				SettingsImpl.START_POSITION_LAT, 0);
		int longitude = this.sharedPreferences.getInt(
				SettingsImpl.START_POSITION_LONG, 0);
		return new GeoPoint(latitude, longitude);
	}

	public String getStartAddress() {
		// TODO: use the default country of the user, or maybe the current city
		return this.sharedPreferences.getString(
				SettingsImpl.START_POSITION_ADDRESS, "");
	}

	public boolean isUseStartAddress() {
		return this.sharedPreferences.getBoolean(
				SettingsImpl.START_POSITION_DEFAULT, true);
	}

	public void setStartPosition(GeoPoint point) {
		Editor editor = this.sharedPreferences.edit();
		editor.putInt(SettingsImpl.START_POSITION_LONG, point.getLongitudeE6());
		editor.putInt(SettingsImpl.START_POSITION_LAT, point.getLongitudeE6());
		editor.putBoolean(SettingsImpl.CHECK_POSITION_POINT, true);
		editor.commit();
	}

	public void setStartAddress(String address) {
		Editor editor = this.sharedPreferences.edit();
		editor.putString(SettingsImpl.START_POSITION_ADDRESS, address);
		editor.putBoolean(SettingsImpl.CHECK_POSITION_ADDRESS, true);
		editor.commit();
	}

	public void setUseStartAddress(boolean useAddress) {
		Editor editor = this.sharedPreferences.edit();
		editor.putBoolean(SettingsImpl.START_POSITION_DEFAULT, useAddress);
		editor.commit();
	}

	public boolean isSettingSet(String setting) {
		if(setting.equals(SettingsImpl.CHECK_POSITION_ADDRESS) || 
				setting.equals(SettingsImpl.CHECK_POSITION_POINT)) {
			return this.sharedPreferences.getBoolean(setting, false);
		} else {
			throw new NoSuchSettingPresentException("Maybe you forget to change the method isSettingSet?");
		}
	}

	public void deleteSetting(String setting) {
		Editor editor = this.sharedPreferences.edit();
		if (setting.equals(SettingsImpl.CHECK_POSITION_ADDRESS)) {
			editor.putString(SettingsImpl.START_POSITION_ADDRESS, "");
			editor.putBoolean(SettingsImpl.CHECK_POSITION_ADDRESS, false);
		}
		if(setting.equals(SettingsImpl.CHECK_POSITION_POINT) == true) {
			editor.putInt(SettingsImpl.START_POSITION_LAT, 0);
			editor.putInt(SettingsImpl.START_POSITION_LONG, 0);
			editor.putBoolean(SettingsImpl.CHECK_POSITION_POINT, false);
		}
		editor.commit();
	}
}
